//
//  ZPMineQRCodeViewController.m
//  ZPIOSEducation
//
//  Created by mini1 on 2017/1/10.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "ZPMineQRCodeViewController.h"
#import "JCBrightness.h"


@interface ZPMineQRCodeViewController ()
{
    
}
@property (weak, nonatomic) IBOutlet UIImageView *QRCodeImgV;
//@property (weak, nonatomic) IBOutlet UITextField *QRCodeContent;
//@property (weak, nonatomic) IBOutlet UIButton    *QRCodeBtn1;
//@property (weak, nonatomic) IBOutlet UIButton    *QRCodeBtn2;
@end

@implementation ZPMineQRCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.title = @"我的二维码";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhenpin_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)];
    // 屏幕调最亮
    [JCBrightness graduallySetBrightness:1];
    
    NSString  *stuNum = [[ZPAuthStore  sharedZPAuthStore].userToDic objectForKey:@"stuNum"];
    if (stuNum && ![stuNum  isEqual:@""]) {
        [self  qrCodeWithStr:[NSString  stringWithFormat:@"%@",stuNum]];
    }
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    // 回复屏幕亮度
    [JCBrightness graduallyResumeBrightness];
}


- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}


// 普通黑白二维码
- (void)qrCodeWithStr:(NSString  *)string
{
    //获取内建的所有过滤器.
//    NSArray *filterArr = [CIFilter filterNamesInCategories:@[kCICategoryBuiltIn]];
//    ZPLog(@"%@",filterArr);
    
    // 1.实例化二维码滤镜
    CIFilter *qrImageFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
    [qrImageFilter setDefaults];
    
    // 3.将字符串转换成 NSdata (虽然二维码本质上是 字符串,但是这里需要转换,不转换就崩溃)
    NSData *qrImageData = [string dataUsingEncoding:NSUTF8StringEncoding];
    //我们可以打印,看过滤器的 输入属性.这样我们才知道给谁赋值
//    ZPLog(@"%@",qrImageFilter.inputKeys);
    /*
     inputMessage,        //二维码输入信息
     inputCorrectionLevel //二维码错误的等级,就是容错率
     */
    
    // 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
    [qrImageFilter setValue:qrImageData forKey:@"inputMessage"];
    
    // 5.生成二维码
    CIImage *qrImage = [qrImageFilter outputImage];
    //但是图片 发现有的小 (27,27),我们需要放大..我们进去CIImage 内部看属性
    qrImage = [qrImage imageByApplyingTransform:CGAffineTransformMakeScale(9, 9)];
    
    
    self.QRCodeImgV.image = [UIImage imageWithCIImage:qrImage];
    //    self.imageView.image = [UIImage imageWithCIImage:qrImage scale:100.0 orientation:UIImageOrientationUp];
    

    self.QRCodeImgV.layer.shadowOffset=CGSizeMake(0, 5);//设置阴影的偏移量
    
    self.QRCodeImgV.layer.shadowRadius=1;//设置阴影的半径
    
    self.QRCodeImgV.layer.shadowColor=[UIColor clearColor].CGColor;//设置阴影的颜色为红色
    
    self.QRCodeImgV.layer.shadowOpacity=0.3;

}
// 生成中间带图片二维码
- (void)qrImageCode
{
    //
//    NSArray *filterArr = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
//    ZPLog(@"%@",filterArr);
    
    //二维码过滤器
    CIFilter *qrImageFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    //设置过滤器默认属性
    [qrImageFilter setDefaults];
    
    //将字符串转换成 NSdata (虽然二维码本质上是 字符串,但是这里需要转换,不转换就崩溃)
    NSData *qrImageData = [@"文本框内容" dataUsingEncoding:NSUTF8StringEncoding];
    
    //我们可以打印,看过滤器的 输入属性.这样我们才知道给谁赋值
//    NSLog(@"%@",qrImageFilter.inputKeys);
    /*
     inputMessage,        //二维码输入信息
     inputCorrectionLevel //二维码错误的等级,就是容错率
     */
    
    
    //设置过滤器的 输入值  ,KVC赋值
    [qrImageFilter setValue:qrImageData forKey:@"inputMessage"];
    
    //取出图片
    CIImage *qrImage = [qrImageFilter outputImage];
    
    //但是图片 发现有的小 (27,27),我们需要放大..我们进去CIImage 内部看属性
    qrImage = [qrImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)];
    
    //转成 UI的 类型
    UIImage *qrUIImage = [UIImage imageWithCIImage:qrImage];
    
    
    //----------------给 二维码 中间增加一个 自定义图片----------------
    //开启绘图,获取图形上下文  (上下文的大小,就是二维码的大小)
    UIGraphicsBeginImageContext(qrUIImage.size);
    
    //把二维码图片画上去. (这里是以,图形上下文,左上角为 (0,0)点)
    [qrUIImage drawInRect:CGRectMake(0, 0, qrUIImage.size.width, qrUIImage.size.height)];
    
    
    //再把小图片画上去
    UIImage *sImage = [UIImage imageNamed:@"girl_image.png"];
    
    CGFloat sImageW = 100;
    CGFloat sImageH= sImageW;
    CGFloat sImageX = (qrUIImage.size.width - sImageW) * 0.5;
    CGFloat sImgaeY = (qrUIImage.size.height - sImageH) * 0.5;
    
    [sImage drawInRect:CGRectMake(sImageX, sImgaeY, sImageW, sImageH)];
    
    //获取当前画得的这张图片
    UIImage *finalyImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    //设置图片
    self.QRCodeImgV.image = finalyImage;
    
    self.QRCodeImgV.layer.shadowOffset=CGSizeMake(0, 5);//设置阴影的偏移量
    
    self.QRCodeImgV.layer.shadowRadius=1;//设置阴影的半径
    
    self.QRCodeImgV.layer.shadowColor=[UIColor redColor].CGColor;//设置阴影的颜色为红色
    
    self.QRCodeImgV.layer.shadowOpacity=0.3;

}


//- (IBAction)QRCodeBtn1Click:(id)sender
//{
//    [self qrCodeWithStr:self.QRCodeContent.text];
//}
//- (IBAction)QRCodeBtn2Click:(id)sender
//{
//    [self qrImageCode];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
